﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace 图书管理系统
{
    public partial class 荐购 : Form
    {

        public 荐购()
        {
            InitializeComponent();
        }

        private void 荐购_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            string sql = "select RecmdatID,RecmdatBook,Author,Press,Recmdat_time,Recmdat,Note from Purchaserecord_Table";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                conn.Open();
                DataTable ds = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                foreach (DataRow dataRow in ds.Rows)
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[0].Value = dataRow["RecmdatID"].ToString();
                    dataGridView1.Rows[index].Cells[1].Value = dataRow["RecmdatBook"].ToString();
                    dataGridView1.Rows[index].Cells[2].Value = dataRow["Author"].ToString();
                    dataGridView1.Rows[index].Cells[3].Value = dataRow["Press"].ToString();
                    dataGridView1.Rows[index].Cells[4].Value = dataRow["Recmdat_time"].ToString();
                    dataGridView1.Rows[index].Cells[5].Value = dataRow["Recmdat"].ToString();
                    dataGridView1.Rows[index].Cells[6].Value = dataRow["Note"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string text1 = textBox1.Text.Trim();
            string text2 = textBox2.Text.Trim();
            string text3 = textBox5.Text.Trim();
            
            if (text1 == string.Empty|| text2 == string.Empty || text3 == string.Empty)
            {
                MessageBox.Show("信息不完整，请输入完整", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SqlConnection conn = new SqlConnection();
                
                string sql = "insert into Purchaserecord_Table(ReaderID,Recmdat_time,RecmdatBook,Author,Publication_time,Press,Recmdat,ISBN,Reason) values(@ReaderID,@Recmdat_time,@RecmdatBook,@Author,@Publication_time,@Press,@Recmdat,@ISBN,@Reason)";
                SqlCommand cmd = Dao.SqlConnect(sql, conn);
                string sql2 = "Select * from Binfo_Table where ISBN like '" + text3 + "' and BookName like'" + text1 + "' and Author like'" + text2 + "'";
                SqlConnection conn1 = new SqlConnection();
                SqlCommand cmd1 = Dao.SqlConnect(sql2, conn1);


                try
                {
                    conn.Open();
                    conn1.Open();
                    DataSet dt = new DataSet();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd1);
                    sqlDataAdapter.Fill(dt, "Table_1");
                    SqlDataReader sdr = cmd1.ExecuteReader();
                    if (!sdr.HasRows)
                    {
                        try
                        {
                            String s = DateTime.Now.ToLocalTime().ToString();
                            User user = 用户登录.user;
                            String id = user.Id;   
                            cmd.Parameters.Add("@ReaderID", SqlDbType.Char,15).Value = id;
                            cmd.Parameters.Add("@Recmdat_time", SqlDbType.DateTime).Value = s;
                            cmd.Parameters.Add("@RecmdatBook", SqlDbType.Char, 30).Value = text1;
                            cmd.Parameters.Add("@Author", SqlDbType.VarChar, 100).Value = textBox2.Text;
                            cmd.Parameters.Add("@Press", SqlDbType.VarChar, 100).Value = textBox3.Text;
                            cmd.Parameters.Add("@Publication_time", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                            cmd.Parameters.Add("@Recmdat", SqlDbType.Char, 30).Value = "未审核";
                            cmd.Parameters.Add("@ISBN", SqlDbType.VarChar, 30).Value = text3;
                            cmd.Parameters.Add("@Reason", SqlDbType.VarChar, 50).Value = textBox6.Text;
                            int i = cmd.ExecuteNonQuery();
                            MessageBox.Show("荐购成功！！");

                            textBox1.Text = "";
                            textBox2.Text = "";
                            textBox3.Text = "";
                            textBox5.Text = "";
                            textBox6.Text = "";
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            conn.Close();
                        }

                    }
                    else
                    {
                        MessageBox.Show("书库已存在该书籍！！");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn1.Close();
                }

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 0)//也可以判断tabControl1.SelectedTab.Text的值
            {
                User user = 用户登录.user;
                String id = user.Id;

                if (id == null)
                {
                    用户登录 form = new 用户登录("recmdat");
                    MessageBox.Show("未登录，请先登录");
                    this.Close();
                    form.TopMost = true;
                    form.Show();
                }
               

            }

            if(tabControl1.SelectedIndex == 0)
            {
                dataGridView1.Rows.Clear();
                SqlConnection conn = new SqlConnection();
                string sql = "select RecmdatID,RecmdatBook,Author,Press,Recmdat_time,Recmdat,Note from Purchaserecord_Table";
                SqlCommand cmd = Dao.SqlConnect(sql, conn);
                try 
                {
                conn.Open();
                DataTable ds = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                    foreach (DataRow dataRow in ds.Rows)
                {
                        int index = dataGridView1.Rows.Add();
                        dataGridView1.Rows[index].Cells[0].Value = dataRow["RecmdatID"].ToString();
                        dataGridView1.Rows[index].Cells[1].Value = dataRow["RecmdatBook"].ToString();
                        dataGridView1.Rows[index].Cells[2].Value = dataRow["Author"].ToString();
                        dataGridView1.Rows[index].Cells[3].Value = dataRow["Press"].ToString();
                        dataGridView1.Rows[index].Cells[4].Value = dataRow["Recmdat_time"].ToString();
                        dataGridView1.Rows[index].Cells[5].Value = dataRow["Recmdat"].ToString();
                        dataGridView1.Rows[index].Cells[6].Value = dataRow["Note"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            User user = 用户登录.user;
            this.Hide();
            首页 form = new 首页(user);
            form.Show();
        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }
    }
}
